package view;

import java.awt.EventQueue;
import java.io.Serializable;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import controller.OpProcessos;
import javax.swing.JButton;
import javax.swing.JTable;

public class Tela extends JFrame implements Serializable  {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField tfNomeSo;
	private JTable tblProc;


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Tela frame = new Tela();
					frame.setVisible(true);
					frame.setTitle("Gerenciador de Tarefas");
					frame.setResizable(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Tela() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 584, 614);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblSistemaOperacional = new JLabel("Sistema Operacional");
		lblSistemaOperacional.setBounds(10, 11, 137, 14);
		contentPane.add(lblSistemaOperacional);

		tfNomeSo = new JTextField();
		tfNomeSo.setEditable(false);
		tfNomeSo.setBounds(148, 8, 412, 20);
		contentPane.add(tfNomeSo);
		tfNomeSo.setColumns(10);
		
		JButton btnAtualizar = new JButton("Atualizar");
		btnAtualizar.setBounds(10, 36, 89, 23);
		contentPane.add(btnAtualizar);
		
		JScrollPane spTblProc = new JScrollPane();
		spTblProc.setBounds(10, 71, 557, 499);
		contentPane.add(spTblProc);
		
		tblProc = new JTable();
		tblProc.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"New column"
			}
		));
		spTblProc.setViewportView(tblProc);
		tblProc.setSelectionMode(JTable.WHEN_FOCUSED);
		
		
		
		final OpProcessos op = new OpProcessos(tfNomeSo, tblProc);
		String so = op.verificaSO();
		
		if (so.contains("inux")){
//			taProcessos.setFont(new Font("Courier 10 Pitch", Font.PLAIN, 11));
			lblSistemaOperacional.setFont(new Font("Courier 10 Pitch", Font.PLAIN, 12));
			tblProc.setFont(new Font("Courier 10 Pitch", Font.PLAIN, 12));
			JTableHeader cabecalho = tblProc.getTableHeader();  
			cabecalho.setFont(new Font("Courier 10 Pitch", Font.PLAIN, 12));
			btnAtualizar.setBounds(10, 36, 100, 23);
			((DefaultTableCellRenderer)tblProc.getTableHeader().getDefaultRenderer())
		    .setHorizontalAlignment(SwingConstants.LEFT);
		} else {
//			taProcessos.setFont(new Font("Courier New", Font.PLAIN, 11));
			tblProc.setFont(new Font("Courier New", Font.PLAIN, 11));
			JTableHeader cabecalho = tblProc.getTableHeader();  
			cabecalho.setFont(new Font("Courier New", Font.PLAIN, 11));
		}
		
		
		if (tblProc.getRowCount() == 0){
			op.listaProcessos();
		}
		
		ActionListener acaoAtualizar = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				op.atualizarListas();
			}
		};
		
		MouseListener acaoClicarTabela = new MouseListener() {
			
			@Override
			public void mouseClicked(MouseEvent e) {
				op.mataProcesso();
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mousePressed(MouseEvent e) {
			}

			@Override
			public void mouseReleased(MouseEvent e) {
			}
		};
		
		btnAtualizar.addActionListener(acaoAtualizar);
		tblProc.addMouseListener(acaoClicarTabela);
	}
}
